package com.example.friendcash.activities;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.*;
import android.support.v4.view.ViewPager;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.astuetz.PagerSlidingTabStrip;
import com.example.friendcash.R;
import com.example.friendcash.components.ViewPagerParallax;
import com.example.friendcash.database.core.Friend;
import com.example.friendcash.fragments.BaseFragment;
import com.example.friendcash.fragments.ExpensesFragment;
import com.example.friendcash.fragments.FriendsFragment;
import com.example.friendcash.fragments.PaybackFragment;
import com.example.friendcash.parameters.IntentConstants;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class WorkingActivity extends SherlockFragmentActivity {

    private MyAdapter adapter;

    private final List<BaseFragment> fragments = Arrays.asList(
        new ExpensesFragment(),
        new FriendsFragment(),
        new PaybackFragment()
    );

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.working_activity);
        adapter = new MyAdapter(getSupportFragmentManager());
        final ViewPagerParallax pager = (ViewPagerParallax) findViewById(R.id.pager);
        pager.setBackgroundAsset(R.drawable.background);
        pager.setMaxPages(adapter.getCount());
        pager.setAdapter(adapter);
        final PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        tabs.setViewPager(pager);
        tabs.setTextColor(Color.rgb(255, 255, 255));
        tabs.setIndicatorColor(Color.parseColor("#FF96AA39"));
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            final ArrayList<String> selectedFriends = data.getStringArrayListExtra(IntentConstants.NAMES);
            final List<Friend> friends = convertToFriendObjects(selectedFriends);
            ((FriendsFragment) adapter.getItem(1)).addFriendsAndUpdateView(friends) ;
        }
    }

    private List<Friend> convertToFriendObjects(ArrayList<String> names) {
        final List<Friend> friends = new ArrayList<Friend>();
        for (String name : names) {
            friends.add(new Friend(0, name));
        }
        return friends;
    }

    private class MyAdapter extends FragmentStatePagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return fragments.get(position).getTitle();
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }

}
